package com.cos.shop.web.servlet;

import com.cos.shop.common.Const;
import com.cos.shop.common.ResponseCode;
import com.cos.shop.common.ResponseResult;
import com.cos.shop.service.ProductServices;
import com.cos.shop.service.impl.ProductServicesImpl;
import com.cos.shop.util.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: ProductController
 * Author: saber
 * Date:2022/9/18 9:58
 */
@WebServlet("/pro")
public class ProductController extends BaseServlet {

    private ProductServices ps = new ProductServicesImpl();

    public String getGoodsListByTypeId(HttpServletRequest request, HttpServletResponse response){

        int tid = StringUtils.stringToInt(request.getParameter("tid"));
        int page = StringUtils.stringToInt(request.getParameter("page"));

        ResponseResult rr = ps.getProductByPage(tid,page);

        if (rr.getCode()== ResponseCode.SUCCESS.getCode()) {
            request.setAttribute("rr",rr);
            return Const.FORWARD+ "/goodsList.jsp";
        }else {
            request.setAttribute("rr",rr);
            return Const.FORWARD+ "/message.jsp";
        }

    }

    public String getGoodsById(HttpServletRequest request, HttpServletResponse response){

        int pid = StringUtils.stringToInt(request.getParameter("pid"));

        ResponseResult rr = ps.getProductById(pid);

        if (rr.getCode()== ResponseCode.SUCCESS.getCode()) {
            request.setAttribute("rr",rr);
            return Const.FORWARD+"/goodsDetail.jsp";
        }else {
            request.setAttribute("rr",rr);
            return Const.FORWARD+ "/message.jsp";
        }
    }

} 
